package com.example.reagent_management.ui.statistics;

import android.graphics.Color;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.TextView;

import com.example.lib_base.base.BaseDBFragment;
import com.example.lib_base.base.DataBindingConfig;
import com.example.reagent_management.R;
import com.example.reagent_management.databinding.FragmentStatisticsBinding;
import com.example.reagent_management.ui.statistics.helper.StateFragmentAdapter;
import com.example.reagent_management.ui.statistics.pojo.ReagentNameAndNum;
import com.github.mikephil.charting.charts.BarChart;
import com.github.mikephil.charting.components.AxisBase;
import com.github.mikephil.charting.components.Description;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.components.YAxis;
import com.github.mikephil.charting.data.BarData;
import com.github.mikephil.charting.data.BarDataSet;
import com.github.mikephil.charting.data.BarEntry;
import com.github.mikephil.charting.formatter.IAxisValueFormatter;
import com.github.mikephil.charting.formatter.ValueFormatter;
import com.github.mikephil.charting.interfaces.datasets.IBarDataSet;

import java.util.ArrayList;
import java.util.List;

import androidx.core.content.ContextCompat;
import androidx.fragment.app.Fragment;
import androidx.viewpager2.widget.ViewPager2;

import dagger.hilt.android.AndroidEntryPoint;

import static com.example.reagent_management.ui.statistics.helper.ChartHelperKt.initBarChart;
import static com.example.reagent_management.ui.statistics.helper.ChartHelperKt.refreshBarData;

@AndroidEntryPoint
public class StatisticsFragment extends BaseDBFragment {

    private StatisticsViewModel mState;
    private BarChart chartMonth;
    private List<BarEntry> yVls;
    private String[] xVls;

    @Override
    protected DataBindingConfig getDataBindingConfig() {
        return new DataBindingConfig(R.layout.fragment_statistics);
    }

    @Override
    protected void initViewModel() {
        mState = getFragmentScopeViewModel(StatisticsViewModel.class);
    }

    @Override
    protected void initViewOrObserve() {
        final FragmentStatisticsBinding binding = (FragmentStatisticsBinding) getBinding();
        //月出库试剂名前五名
        chartMonth = binding.monthCHart;
        initBarChart(chartMonth);
        mState.getReagentNum();
        mState.getMonthData().observe(getViewLifecycleOwner(), data -> {
            if (yVls == null) {
                yVls = new ArrayList<>();
            }
            if (xVls == null) {
                xVls = new String[5];
            }
            //优化 每次更改数值即可不清除 TODO 已修改 考虑到有可随时的回退操作删除原来的优化
            yVls.clear();
            for (int i = 0; i < data.size(); i++) {
                ReagentNameAndNum reagent = data.get(i);
                BarEntry entry = new BarEntry(i + 1, reagent.getNum());
                xVls[i] = data.get(i).getReagentName();
                yVls.add(entry);
            }
            if (!data.isEmpty()) {
                refreshBarData(chartMonth, yVls, xVls, this.requireContext());
            } else {
                for (int i = 0; i < xVls.length; i++) {
                    if (xVls[i] != null){
                        xVls[i] = "";
                    }
                }
            }
            //有时出现没刷新界面的情况，再次调用刷新
            chartMonth.invalidate();
        });
        //24小时 近7天 viewPager2可用 viewHolder 或 fragment
        final List<Fragment> fragments = new ArrayList<>();
        fragments.add(LineChartFragment.Companion.create(1));
        fragments.add(LineChartFragment.Companion.create(2));
        binding.viewPager2.setAdapter(new StateFragmentAdapter(this, fragments));
        binding.viewPager2.setUserInputEnabled(false);  //viewPager2不拦截任何事件
        initSpinner(binding.spinner,binding.viewPager2);
    }

    private void initSpinner(final Spinner sp,final ViewPager2 viewPager2){
        //声明一个下拉列表的数组适配器
//        ArrayAdapter<String> starAdapter = new ArrayAdapter<String>(this.requireContext(), R.layout.item_select, starArray);
        //设置数组适配器的布局样式
//        starAdapter.setDropDownViewResource(R.layout.item_dropdown);
//        sp.setAdapter(starAdapter);
        sp.setSelection(0);
        sp.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                if (parent.getChildAt(0) != null) {
                    ((TextView) parent.getChildAt(0)).setTextColor(Color.RED);
                    ((TextView) parent.getChildAt(0)).setTextSize(20);
                    viewPager2.setCurrentItem(position, false);
                }
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });
    }
}

